﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Connect
{
    public partial class Backup : Form
    {
        public Backup()
        {
            InitializeComponent();
            cn = tempcn.GetConnect();
        }

        Connection tempcn = new Connection();
        SqlConnection cn = new SqlConnection();

        private void cancel_Click(object sender, EventArgs e)
        {
            try
            {
                Close();
            }
            catch (Exception)
            {

                Application.ExitThread();
            }
        }
  
        SaveFileDialog savedg = new SaveFileDialog();
        private void back_Click(object sender, EventArgs e)
        {
             
                savedg.Filter = "Tập tin sao lưu (*.bak)|*.bak|Tất cả các dạng tệp tin (*.*)|*.*";
                savedg.AddExtension = true;
                savedg.DefaultExt = "bak";
                savedg.RestoreDirectory = true;
                savedg.InitialDirectory = "D:\\";
                savedg.Title = "Tao tap tin sao luu tai cac thoi diem khac nhau";
                savedg.FilterIndex = 1;
                if (savedg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    lin = savedg.FileName.Trim();
                    backgroundWorker1.RunWorkerAsync();
                }

        }
        string lin = "";


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                            if (string.IsNullOrEmpty(lin) == true)
                            {
                                return;
                            }             
                            else
                            {
                            Connection h = new Connection();
                                cn.Open();
                                lin = cnSQL.Backup(savedg, cn.Database);
                                    SqlCommand cm1 = new SqlCommand();
                                    System.Data.SqlClient.SqlConnection.ClearAllPools();
                                    cm1.CommandText = lin;
                                    SqlConnection con = new SqlConnection(String.Format("server={0};database=master;Integrated Security=True", h.rosv()));
                                    con.Open();
                                    cm1.Connection = con;
                                    cm1.ExecuteNonQuery();

                                MessageBox.Show("Sao lưu CSDL thành công", "Thành công");
                             }

                         
            }
            catch (Exception ex)
            {
                    MessageBox.Show("Sao lưu thất bại : " + ex.Message , "Thất bại");
             }
                  cancel_Click(sender, e);

        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
                                  progressBar1.Value = 100;
        }


    }
}
